using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace SiKuma.ScreenSystemLibrary
{
    public class InputSystem
    {
        public const float triggerThreshold = 0.2f;

        public static KeyboardState CurrentKeyboardState
        {
            get;
            private set;
        }

        public static KeyboardState PreviousKeyboardState
        {
            get;
            private set;
        }

        public static GamePadState CurrentGamepadState
        {
            get;
            private set;
        }

        public static GamePadState PreviousGamepadState
        {
            get;
            private set;
        }

        public static MouseState CurrentMouseState
        {
            get;
            private set;
        }

        public static MouseState PreviousMouseState
        {
            get;
            private set;
        }

        public static bool IsPressedKey(Keys k)
        {
            return InputSystem.CurrentKeyboardState.IsKeyDown(k);
        }

        public static bool IsNewKeyPress(Keys k)
        {
            return InputSystem.CurrentKeyboardState.IsKeyDown(k) && InputSystem.PreviousKeyboardState.IsKeyUp(k);
        }

        public static bool IsHeldKey(Keys k)
        {
            return InputSystem.CurrentKeyboardState.IsKeyDown(k) && InputSystem.PreviousKeyboardState.IsKeyDown(k);
        }

        public static bool IsPressedButton(Buttons b)
        {
            return b != (Buttons)0 && InputSystem.CurrentGamepadState.IsButtonDown(b);
        }

        public static bool IsNewButtonPress(Buttons b)
        {
            return b != (Buttons)0 && InputSystem.CurrentGamepadState.IsButtonDown(b) && InputSystem.PreviousGamepadState.IsButtonUp(b);
        }

        public static bool IsHeldButton(Buttons b)
        {
            return b != (Buttons)0 && InputSystem.CurrentGamepadState.IsButtonDown(b) && InputSystem.PreviousGamepadState.IsButtonDown(b);
        }

        public static bool IsPressedMouse(MousePresses m)
        {
            if (m == MousePresses.LeftMouse)
            {
                return InputSystem.CurrentMouseState.LeftButton == ButtonState.Pressed;
            }
            return m == MousePresses.RightMouse && InputSystem.CurrentMouseState.RightButton == ButtonState.Pressed;
        }

        public static bool IsNewMousePress(MousePresses m)
        {
            if (m == MousePresses.LeftMouse)
            {
                return InputSystem.CurrentMouseState.LeftButton == ButtonState.Pressed && InputSystem.PreviousMouseState.LeftButton == ButtonState.Released;
            }
            return m == MousePresses.RightMouse && InputSystem.CurrentMouseState.RightButton == ButtonState.Pressed && InputSystem.PreviousMouseState.RightButton == ButtonState.Released;
        }

        public static bool IsHeldMousePress(MousePresses m)
        {
            if (m == MousePresses.LeftMouse)
            {
                return InputSystem.CurrentMouseState.LeftButton == ButtonState.Pressed && InputSystem.PreviousMouseState.LeftButton == ButtonState.Pressed;
            }
            return m == MousePresses.RightMouse && InputSystem.CurrentMouseState.RightButton == ButtonState.Pressed && InputSystem.PreviousMouseState.RightButton == ButtonState.Pressed;
        }

        public static float LeftTrigger()
        {
            return InputSystem.CurrentGamepadState.Triggers.Left;
        }

        public static float RightTrigger()
        {
            return InputSystem.CurrentGamepadState.Triggers.Right;
        }

        public static Vector2 MousePosition()
        {
            return new Vector2((float)InputSystem.CurrentMouseState.X, (float)InputSystem.CurrentMouseState.Y);
        }

        public InputSystem()
        {
            InputSystem.CurrentGamepadState = default(GamePadState);
            InputSystem.PreviousGamepadState = default(GamePadState);
            InputSystem.CurrentKeyboardState = default(KeyboardState);
            InputSystem.PreviousKeyboardState = default(KeyboardState);
            InputSystem.CurrentMouseState = default(MouseState);
            InputSystem.PreviousMouseState = default(MouseState);
        }

        public static void Update(GameTime gameTime)
        {
            InputSystem.PreviousGamepadState = InputSystem.CurrentGamepadState;
            InputSystem.CurrentGamepadState = GamePad.GetState(PlayerIndex.One);
            InputSystem.PreviousKeyboardState = InputSystem.CurrentKeyboardState;
            InputSystem.CurrentKeyboardState = Keyboard.GetState();
            InputSystem.PreviousMouseState = InputSystem.CurrentMouseState;
            InputSystem.CurrentMouseState = Mouse.GetState();
        }
    }
}
